package com.ms.util;

import java.util.Calendar;
import java.util.Date;

public class DateUtil {

	/**
	 * 由年、季度、月得到时间段
	 * @param year
	 * @param quarter 0: 表示全部季度
	 * @param month 0: 表示全部月份
	 * @return
	 */
	public static Date[] getTimeRange(Integer year, Integer quarter, Integer month) {
		if(year == null) year = 0;
		if(quarter == null) quarter = 0;
		if(month == null) month = 0;
		
		if(year.intValue() == 0) {
			return null;
		}
		
		Calendar cal = Calendar.getInstance();
		cal.clear();
		cal.set(Calendar.YEAR, year);
		
		Calendar cal2 = Calendar.getInstance();
		cal2.clear();
		cal2.set(Calendar.YEAR, year);
		cal2.set(Calendar.MONTH, Calendar.DECEMBER);
		cal2.set(Calendar.HOUR_OF_DAY, 23);
		cal2.set(Calendar.MINUTE, 59);
		cal2.set(Calendar.SECOND, 59);
		
		if(month > 0) {
			cal.set(Calendar.MONTH, month - 1);
			cal2.set(Calendar.MONTH, month - 1);
		}
		else if(quarter > 0) {
			int startMonth = (quarter - 1) * 3 + 1;
			int endMonth = quarter * 3;
			
			cal.set(Calendar.MONTH, startMonth - 1);
			cal2.set(Calendar.MONTH, endMonth - 1);
		}
		
		int lastDay = cal2.getActualMaximum(Calendar.DATE);
		cal2.set(Calendar.DAY_OF_MONTH, lastDay);
		
		Date startTime = cal.getTime();
		Date endTime = cal2.getTime();
		
//		System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(startTime));
//		System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(endTime));
		
		Date[] retDate = new Date[2];
		retDate[0] = startTime;
		retDate[1] = endTime;
		
		return retDate;
	}
	
	/**
	 * 得到环比时间范围
	 * @param year
	 * @param quarter
	 * @param month
	 * @return
	 */
	public static Date[] getTimeRangeHan(int year, int quarter, int month) {
		Calendar calStart = Calendar.getInstance();
		calStart.clear();
		calStart.set(Calendar.YEAR, year);
		
		Calendar calEnd = Calendar.getInstance();
		calEnd.clear();
		calEnd.set(Calendar.YEAR, year);
		calEnd.set(Calendar.MONTH, Calendar.DECEMBER);
		calEnd.set(Calendar.HOUR_OF_DAY, 23);
		calEnd.set(Calendar.MINUTE, 59);
		calEnd.set(Calendar.SECOND, 59);
		
		if(month > 0) {//选择了月份
			calStart.set(Calendar.MONTH, month - 1);//当前开始时间
			calEnd.set(Calendar.MONTH, month - 1);//当前结束时间
			
			//月环比，开始结束时间是前一月
			calStart.add(Calendar.MONTH, -1);
			calEnd.add(Calendar.MONTH, -1);
		}
		else if(quarter > 0) {//选择整季度
			int startMonth = (quarter - 1) * 3 + 1;
			int endMonth = quarter * 3;
			
			calStart.set(Calendar.MONTH, startMonth - 1);//当前开始时间
			calEnd.set(Calendar.MONTH, endMonth - 1);//当前结束时间
			
			//季度环比，开始结束时间是前三月
			calStart.add(Calendar.MONTH, -3);
			calEnd.add(Calendar.MONTH, -3);
		}
		else {//选择整年
			//年度环比，开始结束时间是前一年
			calStart.add(Calendar.YEAR, -1);
			calEnd.add(Calendar.YEAR, -1);
		}
		
		//设置结束时间的最后一天
		int lastDay = calEnd.getActualMaximum(Calendar.DATE);
		calEnd.set(Calendar.DAY_OF_MONTH, lastDay);
		
		Date startTime = calStart.getTime();
		Date endTime = calEnd.getTime();
		
//		System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(startTime));
//		System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(endTime));
		
		Date[] retDate = new Date[2];
		retDate[0] = startTime;
		retDate[1] = endTime;
		return retDate;
	}
	
	/**
	 * 得到同比时间范围
	 * @param year
	 * @param quarter
	 * @param month
	 * @return
	 */
	public static Date[] getTimeRangeTog(int year, int quarter, int month) {
		Calendar calStart = Calendar.getInstance();
		calStart.clear();
		calStart.set(Calendar.YEAR, year);
		
		Calendar calEnd = Calendar.getInstance();
		calEnd.clear();
		calEnd.set(Calendar.YEAR, year);
		calEnd.set(Calendar.MONTH, Calendar.DECEMBER);
		calEnd.set(Calendar.HOUR_OF_DAY, 23);
		calEnd.set(Calendar.MINUTE, 59);
		calEnd.set(Calendar.SECOND, 59);
		
		if(month > 0) {//选择了月份
			calStart.set(Calendar.MONTH, month - 1);//当前开始时间
			calEnd.set(Calendar.MONTH, month - 1);//当前结束时间
		}
		else if(quarter > 0) {//选择整季度
			int startMonth = (quarter - 1) * 3 + 1;
			int endMonth = quarter * 3;
			
			calStart.set(Calendar.MONTH, startMonth - 1);//当前开始时间
			calEnd.set(Calendar.MONTH, endMonth - 1);//当前结束时间
		}
		
		//同比，开始结束时间是前一年
		calStart.add(Calendar.YEAR, -1);
		calEnd.add(Calendar.YEAR, -1);
		
		//设置结束时间的最后一天
		int lastDay = calEnd.getActualMaximum(Calendar.DATE);
		calEnd.set(Calendar.DAY_OF_MONTH, lastDay);
		
		Date startTime = calStart.getTime();
		Date endTime = calEnd.getTime();
		
//		System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(startTime));
//		System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(endTime));
		
		Date[] retDate = new Date[2];
		retDate[0] = startTime;
		retDate[1] = endTime;
		return retDate;
	}
	
	/**
	 * 判断是否是本周
	 * @param quarter 季度
	 * @param month 月份
	 * @return
	 */
	public static boolean isWeek(int quarter, int month) {
		Calendar cal = Calendar.getInstance();
		int nowMonth = cal.get(Calendar.MONTH);
		int startMonth ;
		int endMonth ;
		
		if(month > 0) {//选择月份
			startMonth = month;
			endMonth = month;
		}
		else if(quarter > 0) {//选择整季度
			startMonth = (quarter - 1) * 3 + 1;
			endMonth = quarter * 3;
		}
		else {//选择了全年
			startMonth = 1;
			endMonth = 12;
		}
		nowMonth++;//从1开始比较！0为全部
		if(startMonth <= nowMonth && endMonth >= nowMonth)
			return true;
		return false;
	}
	
	/**
	 * 得到上周六的时间
	 * @return
	 */
	public static Date getLastSaturdayDate() {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		cal.add(Calendar.DAY_OF_YEAR, -2);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		
		Date retDate = cal.getTime();
		return retDate;
	}
	
	public static void main(String[] asd) {
		System.out.println(DateUtil.isWeek(0, 0));
	}
}
